home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / ui / file.c < prev    next >
C/C++ Source or Header  |  1998-08-08  |  14KB  |  678 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include <fcntl.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. #include <dirent.h>
  23. #include <limits.h>
  24.  
  25. #include "fix.h"
  26. #include "types.h"
  27. #include "gr.h"
  28. #include "key.h"
  29.  
  30. #include "ui.h"
  31. #include "mono.h"
  32.  
  33. #include "mem.h"
  34.  
  35.  
  36. char filename_list[300][13];
  37. char directory_list[100][13];
  38.  
  39. static int error_mode = 0;
  40.  
  41. #ifdef __WATCOMC__
  42. static char *Message[] = {
  43.     "Disk is write protected",
  44.     "Unknown unit",
  45.     "Drive not ready",
  46.     "Unknown command",
  47.     "CRC error in data",
  48.     "Bad drive-request stuct length",
  49.     "Seek error",
  50.     "Unknown media type",
  51.     "Sector not found",
  52.     "Printer out of paper",
  53.     "Write fault",
  54.     "Read fault",
  55.     "General Failure" };
  56.  
  57. int critical_error_handler( unsigned deverr, unsigned errcode, unsigned *devhdr )
  58. {
  59.     int x;
  60.  
  61.     devhdr = devhdr; deverr = deverr; 
  62.  
  63.     if (error_mode==1) return _HARDERR_FAIL;
  64.  
  65.     x = MessageBox( -2, -2, 2, Message[errcode], "Retry", "Fail" );
  66.  
  67.     switch (x)
  68.     {
  69.     case 1: return _HARDERR_RETRY;
  70.     case 2: return _HARDERR_FAIL;
  71.     default: return _HARDERR_FAIL;
  72.     }
  73. }
  74. #endif
  75. void InstallErrorHandler()
  76. {
  77. #ifdef __WATCOMC__
  78.     _harderr((void *) critical_error_handler );
  79. #endif  
  80.     //Above line modified by KRB, added (void *) cast
  81.     //for the compiler.
  82. }
  83.  
  84. void file_sort( int n, char list[][13] )
  85. {
  86.     int i, j, incr;
  87.     char t[14];
  88.  
  89.     incr = n / 2;
  90.     while( incr > 0 )
  91.     {
  92.         for (i=incr; i<n; i++ )
  93.         {
  94.             j = i - incr;
  95.             while (j>=0 )
  96.             {
  97.                 if (strncmp(list[j], list[j+incr], 12) > 0)
  98.                 {
  99.                     memcpy( t, list[j], 13 );
  100.                     memcpy( list[j], list[j+incr], 13 );
  101.                     memcpy( list[j+incr], t, 13 );
  102.                     j -= incr;
  103.                 }
  104.                 else
  105.                     break;
  106.             }
  107.         }
  108.         incr = incr / 2;
  109.     }
  110. }
  111.  
  112.  
  113. int SingleDrive()
  114. {
  115.     int FloppyPresent, FloppyNumber;
  116.     unsigned char b;
  117.  
  118.     b = *((unsigned char *)0x410);
  119.  
  120.     FloppyPresent = b & 1;
  121.     FloppyNumber = ((b&0xC0)>>6)+1;
  122.     if (FloppyPresent && (FloppyNumber==1) )
  123.         return 1;
  124.     else
  125.         return 0;
  126. }
  127.  
  128. void SetFloppy(int d)
  129. {
  130.     if (SingleDrive())
  131.     {
  132.     if (d==1)
  133.         *((unsigned char *)0x504) = 0;
  134.     else if (d==2)
  135.         *((unsigned char *)0x504) = 1;
  136.     }
  137. }
  138.  
  139.  
  140. void file_capitalize( char * s )
  141. {
  142.     while( (*s++ = toupper(*s)) );
  143. }
  144.  
  145.  
  146. #ifdef MSDOS
  147. // Changes to a drive if valid.. 1=A, 2=B, etc
  148. // If flag, then changes to it.
  149. // Returns 0 if not-valid, 1 if valid.
  150. int file_chdrive( int DriveNum, int flag )
  151. {
  152.     unsigned NumDrives, n, org;
  153.     int Valid = 0;
  154.  
  155.     if (!flag)
  156.         _dos_getdrive( &org );
  157.  
  158.     _dos_setdrive( DriveNum, &NumDrives );
  159.     _dos_getdrive( &n );
  160.  
  161.     if (n == DriveNum )
  162.         Valid = 1;
  163.  
  164.     if ( (!flag) && (n != org) )
  165.         _dos_setdrive( org, &NumDrives );
  166.  
  167.     return Valid;
  168. }
  169. #endif
  170.  
  171. // Changes to directory in dir.  Even drive is changed.
  172. // Returns 1 if failed.
  173. //  0 = Changed ok.
  174. //  1 = Invalid disk drive.
  175. //  2 = Invalid directory.
  176.  
  177. int file_chdir( char * dir )
  178. {
  179.     int e;
  180.     char OriginalDirectory[100];
  181. #ifdef MSDOS
  182.     char * Drive;
  183. #endif  
  184.     char * Path;
  185.     char NoDir[] = "/.";
  186.  
  187.     getcwd( OriginalDirectory, 100 );
  188.  
  189. #ifdef MSDOS
  190.     file_capitalize( dir );
  191.  
  192.     Drive = strchr(dir, ':');
  193.  
  194.     if (Drive)
  195.     {
  196.         if (!file_chdrive( *(Drive - 1) - 'A' + 1, 1))
  197.             return 1;
  198.  
  199.         Path = Drive+1;
  200.  
  201.         SetFloppy(*(Drive - 1) - 'A' + 1);
  202.     }
  203.     else
  204. #endif  
  205.     {
  206.         Path = dir;
  207.  
  208.     }
  209.  
  210.     if (!(*Path))
  211.     {
  212.         Path = NoDir;
  213.     }
  214.  
  215.     // This chdir might get a critical error!
  216.     e = chdir( Path );
  217.     if (e)
  218.     {
  219. #ifdef MSDOS
  220.         file_chdrive( OriginalDirectory[0] - 'A'+1, 1 );
  221. #endif      
  222.         return 2;
  223.     }
  224.     return 0;
  225.  
  226. }
  227.  
  228.  
  229. #ifdef MSDOS
  230. int file_getdirlist( int MaxNum, char list[][13] )
  231. {
  232.     struct find_t find;
  233.     int NumDirs = 0, i, CurDrive;
  234.     char cwd[129];
  235.     MaxNum = MaxNum;
  236.  
  237.     getcwd(cwd, 128 );
  238.  
  239.     if (strlen(cwd) >= 4)
  240.     {
  241.         sprintf( list[NumDirs++], ".." );
  242.     }
  243.  
  244.     CurDrive = cwd[0] - 'A' + 1;
  245.  
  246.     if( !_dos_findfirst( "*.", _A_SUBDIR, &find ) )
  247.     {
  248.         if ( find.attrib & _A_SUBDIR )  {
  249.             if (strcmp( "..", find.name) && strcmp( ".", find.name))
  250.                 strncpy(list[NumDirs++], find.name, 13 );
  251.         }
  252.  
  253.         while( !_dos_findnext( &find ) )
  254.         {
  255.             if ( find.attrib & _A_SUBDIR )  {
  256.                 if (strcmp( "..", find.name) && strcmp( ".", find.name))
  257.                 {
  258.                     if (NumDirs==74)
  259.                     {
  260.                         MessageBox( -2,-2, 1, "Only the first 74 directories will be displayed.", "Ok" );
  261.                         break;
  262.                     } else {
  263.                         strncpy(list[NumDirs++], find.name, 13 );
  264.                     }
  265.                 }
  266.             }
  267.         }
  268.     }
  269.  
  270.     file_sort( NumDirs, list );
  271.  
  272.     for (i=1; i<=26; i++ )
  273.     {
  274.         if (file_chdrive(i,0) && (i!=CurDrive))
  275.         {
  276.             if (!((i==2) && SingleDrive()))
  277.                 sprintf( list[NumDirs++], "%c:", i+'A'-1 );
  278.         }
  279.     }
  280.  
  281.     return NumDirs;
  282. }
  283.  
  284. int file_getfilelist( int MaxNum, char list[][13], char * filespec )
  285. {
  286.     struct find_t find;
  287.     int NumFiles = 0;
  288.  
  289.     MaxNum = MaxNum;
  290.  
  291.     if( !_dos_findfirst( filespec, 0, &find ) )
  292.     {
  293.         //if ( !(find.attrib & _A_SUBDIR) )
  294.             strncpy(list[NumFiles++], find.name, 13 );
  295.  
  296.         while( !_dos_findnext( &find ) )
  297.         {
  298.             //if ( !(find.attrib & _A_SUBDIR) )
  299.             //{
  300.                 if (NumFiles==300)
  301.                 {
  302.                     MessageBox( -2,-2, 1, "Only the first 300 files will be displayed.", "Ok" );
  303.                     break;
  304.                 } else {
  305.                     strncpy(list[NumFiles++], find.name, 13 );
  306.                 }
  307.             //}
  308.  
  309.         }
  310.     }
  311.  
  312.     file_sort( NumFiles, list );
  313.  
  314.     return NumFiles;
  315. }
  316. #else
  317. int file_getdirlist( int MaxNum, char list[][13] )
  318. {
  319.     struct stat fileinfo;
  320.     DIR *dir;
  321.     struct dirent *dirent;
  322.     int NumDirs = 0;
  323.     char cwd[PATH_MAX];
  324.  
  325.     getcwd(cwd, 128 );
  326.  
  327.     if (strlen(cwd) >= 2)
  328.     {
  329.         sprintf( list[NumDirs++], ".." );
  330.     }
  331.  
  332.     if ((dir = opendir("."))) {
  333.         while ((dirent = readdir(dir))) {
  334.             if (!stat(dirent->d_name, &fileinfo) && S_ISDIR(fileinfo.st_mode) &&
  335.                 strcmp( "..", dirent->d_name) && strcmp( ".", dirent->d_name)) {
  336.                 if (NumDirs==100) {
  337.                     MessageBox( -2,-2, 1, "Only the first 100 directories will be displayed.", "Ok" );
  338.                     break;
  339.                 } else {
  340.                     list[NumDirs][12] = 0;
  341.                     strncpy(list[NumDirs++], dirent->d_name, 12 );
  342.                 }
  343.             }
  344.         }
  345.     }
  346.  
  347.     file_sort( NumDirs, list );
  348.  
  349.     return NumDirs;
  350. }
  351.  
  352. int file_getfilelist( int MaxNum, char list[][13], char * filespec )
  353. {
  354.     struct stat fileinfo;
  355.     DIR *dir;
  356.     struct dirent *dirent;
  357.     int NumFiles = 0;
  358.  
  359.     if ((dir = opendir("."))) {
  360.         while ((dirent = readdir(dir))) {
  361.             if (!stat(dirent->d_name, &fileinfo) && S_ISREG(fileinfo.st_mode)) {
  362.                 if (NumFiles==300)
  363.                 {
  364.                     MessageBox( -2,-2, 1, "Only the first 300 files will be displayed.", "Ok" );
  365.                     break;
  366.                 } else {
  367.                     list[NumFiles][12] = 0;
  368.                     strncpy(list[NumFiles++], dirent->d_name, 12 );
  369.                 }
  370.             }
  371.         }
  372.     }
  373.  
  374.     file_sort( NumFiles, list );
  375.  
  376.     return NumFiles;
  377. }
  378. #endif
  379.  
  380.  
  381. void split_dir_name(const char *path, char *dir, char *name)
  382. {
  383.     char *p = strrchr(path, '/');
  384.     if (p) {
  385.         if (name) strcpy(name, p + 1);
  386.         if (dir) {
  387.             strncpy(dir, path, p - path);
  388.             dir[p - path] = 0;
  389.         }
  390.     } else {
  391.         if (name) strcpy(name, path);
  392.         if (dir) *dir = 0;
  393.     }
  394. }
  395.  
  396. static int FirstTime = 1;
  397. static char CurDir[128];
  398.  
  399. int ui_get_filename( char * filename, char * Filespec, char * message  )
  400. {
  401.     FILE * TempFile;
  402.     int NumFiles, NumDirs,i;
  403.     char InputText[100];
  404.     char Spaces[35];
  405.     char ErrorMessage[100];
  406.     UI_WINDOW * wnd;
  407.     UI_GADGET_BUTTON * Button1, * Button2, * HelpButton;
  408.     UI_GADGET_LISTBOX * ListBox1;
  409.     UI_GADGET_LISTBOX * ListBox2;
  410.     UI_GADGET_INPUTBOX * UserFile;
  411.     int new_listboxes;
  412.  
  413. #ifdef MSDOS
  414.     char drive[ _MAX_DRIVE ];
  415.     char dir[ _MAX_DIR ];
  416.     char fname[ _MAX_FNAME ];
  417.     char ext[ _MAX_EXT ];
  418. #else   
  419.     char fulldir[PATH_MAX];
  420.     char fullfname[PATH_MAX];
  421. #endif
  422.  
  423.  
  424.     char OrgDir[128];
  425.  
  426.     getcwd( OrgDir, 128 );
  427.  
  428.     if (FirstTime)
  429.         getcwd( CurDir, 128 );
  430.     FirstTime=0;
  431.  
  432.     file_chdir( CurDir );
  433.     
  434.     //MessageBox( -2,-2, 1,"DEBUG:0", "Ok" );
  435.     for (i=0; i<35; i++)
  436.         Spaces[i] = ' ';
  437.     Spaces[34] = 0;
  438.  
  439.     NumFiles = file_getfilelist( 300, filename_list, Filespec );
  440.  
  441.     NumDirs = file_getdirlist( 100, directory_list );
  442.  
  443.     wnd = ui_open_window( 200, 100, 400, 370, WIN_DIALOG );
  444.  
  445.     ui_wprintf_at( wnd, 10, 5, message );
  446.  
  447.     split_dir_name(filename, NULL, InputText);
  448.     {
  449.         char *p = strrchr(filename, '/');
  450.         if (p)
  451.             strcpy(InputText, p + 1);
  452.         else
  453.             strcpy(InputText, filename);
  454.     }
  455.  
  456.     ui_wprintf_at( wnd, 20, 32,"N&ame" );
  457.     UserFile  = ui_add_gadget_inputbox( wnd, 60, 30, 40, 40, InputText );
  458.  
  459.     ui_wprintf_at( wnd, 20, 86,"&Files" );
  460.     ui_wprintf_at( wnd, 210, 86,"&Dirs" );
  461.  
  462.     ListBox1 = ui_add_gadget_listbox( wnd,  20, 110, 125, 200, NumFiles, filename_list[0], 13 );
  463.     ListBox2 = ui_add_gadget_listbox( wnd, 210, 110, 100, 200, NumDirs, directory_list[0], 13 );
  464.  
  465.     Button1 = ui_add_gadget_button( wnd,     20, 330, 60, 25, "Ok", NULL );
  466.     Button2 = ui_add_gadget_button( wnd,    100, 330, 60, 25, "Cancel", NULL );
  467.     HelpButton = ui_add_gadget_button( wnd, 180, 330, 60, 25, "Help", NULL );
  468.  
  469.     wnd->keyboard_focus_gadget = (UI_GADGET *)UserFile;
  470.  
  471.     Button1->hotkey = KEY_CTRLED + KEY_ENTER;
  472.     Button2->hotkey = KEY_ESC;
  473.     HelpButton->hotkey = KEY_F1;
  474.     ListBox1->hotkey = KEY_ALTED + KEY_F;
  475.     ListBox2->hotkey = KEY_ALTED + KEY_D;
  476.     UserFile->hotkey = KEY_ALTED + KEY_A;
  477.  
  478.     ui_gadget_calc_keys(wnd);
  479.  
  480.     ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
  481.     ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
  482.  
  483.     new_listboxes = 0;
  484.  
  485.     while( 1 )
  486.     {
  487.         ui_mega_process();
  488.         ui_window_do_gadgets(wnd);
  489.  
  490.         if ( Button2->pressed )
  491.         {
  492.             file_chdir( OrgDir );
  493.             ui_close_window(wnd);
  494.             return 0;
  495.         }
  496.  
  497.         if ( HelpButton->pressed )
  498.             MessageBox( -1, -1, 1, "Sorry, no help is available!", "Ok" );
  499.  
  500.         if (ListBox1->moved || new_listboxes)
  501.         {
  502.             if (ListBox1->current_item >= 0 )
  503.             {
  504.                 strcpy(UserFile->text, filename_list[ListBox1->current_item] );
  505.                 UserFile->position = strlen(UserFile->text);
  506.                 UserFile->oldposition = UserFile->position;
  507.                 UserFile->status=1;
  508.                 UserFile->first_time = 1;
  509.             }
  510.         }
  511.  
  512.         if (ListBox2->moved || new_listboxes)
  513.         {
  514.             if (ListBox2->current_item >= 0 )
  515.             {
  516.                 if (strrchr( directory_list[ListBox2->current_item], ':' ))
  517.                     sprintf( UserFile->text, "%s%s", directory_list[ListBox2->current_item], Filespec );
  518.                 else
  519.                     sprintf( UserFile->text, "%s/%s", directory_list[ListBox2->current_item], Filespec );
  520.                 UserFile->position = strlen(UserFile->text);
  521.                 UserFile->oldposition = UserFile->position;
  522.                 UserFile->status=1;
  523.                 UserFile->first_time = 1;
  524.             }
  525.         }
  526.         new_listboxes = 0;
  527.  
  528.         if (Button1->pressed || UserFile->pressed || (ListBox1->selected_item > -1 ) || (ListBox2->selected_item > -1 ))
  529.         {
  530.             ui_mouse_hide();
  531.  
  532.             if (ListBox2->selected_item > -1 )  {
  533.                 if (strrchr( directory_list[ListBox2->selected_item], ':' ))
  534.                     sprintf( UserFile->text, "%s%s", directory_list[ListBox2->selected_item], Filespec );
  535.                 else
  536.                     sprintf( UserFile->text, "%s/%s", directory_list[ListBox2->selected_item], Filespec );
  537.             }
  538.  
  539.             error_mode = 1; // Critical error handler automatically fails.
  540.  
  541.             TempFile = fopen( UserFile->text, "r" );
  542.             if (TempFile)
  543.             {
  544.                 // Looks like a valid filename that already exists!
  545.                 fclose( TempFile );
  546.                 break;
  547.             }
  548.  
  549.             // File doesn't exist, but can we create it?
  550.             TempFile = fopen( UserFile->text, "w" );
  551.             if (TempFile)
  552.             {
  553.                 // Looks like a valid filename!
  554.                 fclose( TempFile );
  555.                 remove( UserFile->text );
  556.                 break;
  557.             }
  558.  
  559.             split_dir_name(UserFile->text, fulldir, fullfname);
  560.             
  561.             //mprintf( 0, "----------------------------\n" );
  562.             //mprintf( 0, "Full text: '%s'\n", UserFile->text );
  563.             //mprintf( 0, "Drive: '%s'\n", drive );
  564.             //mprintf( 0, "Dir: '%s'\n", dir );
  565.             //mprintf( 0, "Filename: '%s'\n", fname );
  566.             //mprintf( 0, "Extension: '%s'\n", ext );
  567.             //mprintf( 0, "Full dir: '%s'\n", fulldir );
  568.             //mprintf( 0, "Full fname: '%s'\n", fname );
  569.  
  570.             if (strrchr( fullfname, '?' ) || strrchr( fullfname, '*' ) )    
  571.             {
  572.                 strcat(fulldir, ".");
  573.             } else {
  574.                 strcpy( fullfname, Filespec );
  575.                 strcpy( fulldir, UserFile->text );
  576.             }
  577.  
  578.             //mprintf( 0, "----------------------------\n" );
  579.             //mprintf( 0, "Full dir: '%s'\n", fulldir );
  580.             //mprintf( 0, "Full fname: '%s'\n", fullfname );
  581.  
  582.             if (file_chdir( fulldir )==0)
  583.             {
  584.                 NumFiles = file_getfilelist( 300, filename_list, fullfname );
  585.  
  586.                 strcpy(UserFile->text, fullfname );
  587.                 UserFile->position = strlen(UserFile->text);
  588.                 UserFile->oldposition = UserFile->position;
  589.                 UserFile->status=1;
  590.                 UserFile->first_time = 1;
  591.  
  592.                 NumDirs = file_getdirlist( 100, directory_list );
  593.  
  594.                 ui_listbox_change( wnd, ListBox1, NumFiles, filename_list[0], 13 );
  595.                 ui_listbox_change( wnd, ListBox2, NumDirs, directory_list[0], 13 );
  596.                 new_listboxes = 0;
  597.  
  598.                 getcwd( CurDir, 35 );
  599.                 ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
  600.                 ui_wprintf_at( wnd, 20, 60, "%s", CurDir );
  601.  
  602. /*              i = TICKER;
  603.                 while ( TICKER < i+2 );
  604. */              
  605.  
  606.             }else {
  607.                 sprintf(ErrorMessage, "Error changing to directory '%s'", fulldir );
  608.                 MessageBox( -2, -2, 1, ErrorMessage, "Ok" );
  609.                 UserFile->first_time = 1;
  610.  
  611.             }
  612.  
  613.             error_mode = 0;
  614.  
  615.             ui_mouse_show();
  616.  
  617.         }
  618.     }
  619.  
  620.     //key_flush();
  621.  
  622.     split_dir_name( UserFile->text, fulldir, fullfname);
  623.     
  624.     if ( strlen(fulldir) > 1 )
  625.         file_chdir( fulldir );
  626.     
  627.     getcwd( CurDir, 35 );
  628.         
  629.     if ( strlen(CurDir) > 0 )
  630.     {
  631.             if ( CurDir[strlen(CurDir)-1] == '/' )
  632.                 CurDir[strlen(CurDir)-1] = 0;
  633.     }
  634.         
  635.     sprintf( filename, "%s/%s", CurDir, fullfname );
  636.     //MessageBox( -2, -2, 1, filename, "Ok" );
  637.     
  638.     file_chdir( OrgDir );
  639.         
  640.     ui_close_window(wnd);
  641.  
  642.     return 1;
  643. }
  644.  
  645.  
  646.  
  647. int ui_get_file( char * filename, char * Filespec  )
  648. {
  649.     int x, i, NumFiles;
  650.     char * text[200];
  651.  
  652.     NumFiles = file_getfilelist( 200, filename_list, Filespec );
  653.  
  654.     for (i=0; i< NumFiles; i++ )
  655.     {
  656.         //MALLOC( text[i], char, 15 );//Another compile hack -KRB
  657.         text[i]=(char *)malloc(15*sizeof(char));
  658.         strcpy(text[i], filename_list[i] );
  659.     }
  660.  
  661.     x = MenuX( -1, -1, NumFiles, text );
  662.  
  663.     if ( x > 0 )
  664.         strcpy(filename, filename_list[x-1] );
  665.  
  666.     for (i=0; i< NumFiles; i++ )
  667.     {
  668.         free( text[i] );
  669.     }
  670.  
  671.     if ( x>0 )
  672.         return 1;
  673.     else
  674.         return 0;
  675.  
  676. }
  677.  
  678.